#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    string evaluate(string s, vector<vector<string>> &knowledge)
    {
        unordered_map<string, string> dic;
        for (auto e : knowledge)
        {
            dic[e[0]] = e[1];
        }
        string ret;
        for (int i = 0; i < s.size(); ++i)
        {
            if (s[i] == '(')
            {
                int j = i;
                while (s[j] != ')')
                {
                    ++j;
                }
                if (dic[s.substr(i + 1, j - i - 1)] == "")
                    ret += "?";
                else
                    ret += dic[s.substr(i + 1, j - i - 1)];
                i = j;
            }
            else
            {
                ret += s[i];
            }
        }
        return ret;
    }
};